import { UserActions } from "../actions";
import { IFindUserResponse, IEnrollResponse } from "../models/user";

export interface State {
  findUserResponse: IFindUserResponse | null;
  pending: boolean;
}

export const initialState: State = {
  findUserResponse: {
    code: -1,
    data: {
      username: "",
      id: "",
      email: "",
      phone: ""
    },
    msg: "error"
  },
  pending: false
};

export function reducer(
  state = initialState,
  action: UserActions.UserActionsUnion
): State {
  switch (action.type) {
    case UserActions.UserActionTypes.Login: {
      return {
        ...state,
        pending: true
      };
    }

    case UserActions.UserActionTypes.LoginSuccess: {
      return {
        ...state,
        pending: false
      };
    }

    case UserActions.UserActionTypes.LoginError: {
      return {
        ...state,
        pending: false
      };
    }

    case UserActions.UserActionTypes.FindUser: {
      return {
        ...state,
        pending: true
      };
    }

    case UserActions.UserActionTypes.FindUserSuccess: {
      return {
        ...state,
        findUserResponse: action.payload.res,
        pending: false
      };
    }

    case UserActions.UserActionTypes.FindUserError: {
      return {
        ...state,
        pending: false
      };
    }

    default: {
      return state;
    }
  }
}
